package br.usp.ime.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@SuppressWarnings("serial")
@Entity
@Table(name = "topics")
public class Topic implements Serializable{
	private Integer id;
	private String name;
	private List<User> users;
	private List<Task> tasks;

	@Id
	@GeneratedValue
	@Column(name = "TOPIC_ID")
	public Integer getId(){
		return id;
	}
	public void setId(Integer id){
		this.id = id;
	}
	
	@Column(name = "TOPIC_NAME")
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name = name;
	}
	public void copyTask(Topic topic) {
		this.name = topic.name;
	}

	
    @ManyToMany(targetEntity = br.usp.ime.entities.User.class, cascade = {
    CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.LAZY)
	@org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinTable(name = "users_by_topics", joinColumns = @JoinColumn(name = "TOPIC_ID"), inverseJoinColumns = @JoinColumn(name = "USER_LOGIN"))
    public List<User> getUsers(){
    	return users;
    }
	public void setUsers(List<User> users){
		this.users = users;
	}
	
    @ManyToMany(targetEntity = br.usp.ime.entities.Task.class, cascade = {
    CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch=FetchType.LAZY)
	@org.hibernate.annotations.Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinTable(name = "tasks_by_topics", joinColumns = @JoinColumn(name = "TOPIC_ID"), inverseJoinColumns = @JoinColumn(name = "TASK_ID"))
    public List<Task> getTasks(){
    	return tasks;
    }
	public void setTasks(List<Task> tasks){
		this.tasks = tasks;
	}

	public void addTask(Task task) {
		if (this.tasks == null)
			this.tasks = new ArrayList<Task>();
		this.tasks.add(task);
	}

	public boolean isTask(Task task) {
		return (task != null && tasks != null && tasks.contains(task));
	}	
	

}
